package com.apobates.forum.toro.tuple.strategy;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 *
 * @param <L> 左对象类型
 * @param <R> 右对象类型
 * @param <U> 右函数的参数类型
 *
 * @author xiaofanku@live.cn
 * @since 20220517
 */
public class ManyPairSupplyFutureStrategy<L,R,U> extends AbstractManyPairFutureStrategy<L,R,U> implements ManyPairSupplyStrategy<L,R,U>  {
    private final static Logger logger = LoggerFactory.getLogger(ManyPairSupplyFutureStrategy.class);

    public ManyPairSupplyFutureStrategy(int timeout, TimeUnit unit) {
        super(timeout, unit);
    }

    public ManyPairSupplyFutureStrategy() {
        super();
    }

    @Override
    public Collection<Pair<L, R>> apply(
            Supplier<Collection<L>> leftObjSetFun,
            Function<Collection<U>, Collection<R>> rightFun,
            Map<String, String> argument) {
        // 左
        CompletableFuture<Collection<L>> lFuture = CompletableFuture
                .supplyAsync(()->leftObjSetFun.get())
                .completeOnTimeout(Collections.emptyList(), getTimeout(), getUnit());
        return super.get(lFuture, rightFun, argument);
    }
}
